package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.CnaaFlowQueryDTO;
import com.jeff.tianti.cms.entity.CnaaFlow;
import com.jeff.tianti.cms.service.CnaaFlowService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/channel/cnaaflow")//    证明商标模块/cnaa在线申请流程表后台配置
public class CnaaFlowController {
    @Autowired
    private CnaaFlowService cnaaFlowService;

    @RequestMapping("/list")
    public String cnaaFlowList(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        CnaaFlowQueryDTO dto = new CnaaFlowQueryDTO();
        dto.setPageSize(pageSize);
        dto.setCurrentPage(currentPage);
        PageModel<CnaaFlow> page = cnaaFlowService.queryCnaaFlowPage(dto);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("page", page);
        model.addAttribute(Constants.MENU_NAME, Constants.CNAA_FLOW);
        return "/channel/cnaa/list";
    }

    @RequestMapping("/edit")
    public String cnaaFlowEdit(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        CnaaFlow cnaaFlow = new CnaaFlow();
        if (StringUtils.isNotBlank(id)) {
            cnaaFlow = cnaaFlowService.find(id);
        }
        model.addAttribute(Constants.MENU_NAME, Constants.CNAA_FLOW);
        model.addAttribute("cnaaflow", cnaaFlow);
        return "/channel/cnaa/edit";
    }

    @RequestMapping("/del")
    @ResponseBody
    public AjaxResult cnaaFlowDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            if (StringUtils.isNotBlank(id)) {
                CnaaFlow cnaaFlow = cnaaFlowService.find(id);
                String deleteFlag = cnaaFlow.getDeleteFlag();
                deleteFlag = BaseEntity.DELETE_FLAG_NORMAL.equals(deleteFlag) ? BaseEntity.DELETE_FLAG_DELETED : BaseEntity.DELETE_FLAG_NORMAL;
                cnaaFlow.setDeleteFlag(deleteFlag);
                cnaaFlowService.update(cnaaFlow);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult cnaaFlowSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String level = request.getParameter("level");
            String describe = request.getParameter("describe");
            String flow = request.getParameter("flow");
            if (StringUtils.isNotBlank(id)) {
                CnaaFlow cnaaFlow = cnaaFlowService.find(id);
                cnaaFlow.setDescribe(describe);
                cnaaFlow.setLevel(level);
                cnaaFlow.setFlow(flow);
                cnaaFlowService.update(cnaaFlow);
            } else {
                CnaaFlow cnaaFlow = new CnaaFlow();
                cnaaFlow.setDescribe(describe);
                cnaaFlow.setLevel(level);
                cnaaFlow.setFlow(flow);
                cnaaFlow.setDeleteFlag("0");
                cnaaFlow.setCreateDate(new Date());
                cnaaFlowService.save(cnaaFlow);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
